/*
 * @LastEditors: yuSeen
 * @LastEditTime: 2021-12-26 11:03:59
 */
import { h, render } from "vue";
import { IMessage, messageType } from "./types";
import { random } from 'lodash'
import messageVue from './message.vue'



const base = (type: messageType) => {
  return (message: string, duration: number = 3000) => {
    const VNode = h(
      messageVue,
      {
        messageItem: {
          id: random(1000,999999),
          type,
          message,
          duration
        }
      }
    );
    render(VNode, document.getElementById('messsageBox') as HTMLElement);
  };
};

const Message: IMessage = {
  success: function (message: string, duration?: number): void {
    base("success")(message, duration);
  },
  error: function (message: string, duration?: number): void {
    base("error")(message, duration);
  },
  info: function (message: string, duration?: number): void {
    base("info")(message, duration);
  },
  warning: function (message: string, duration?: number): void {
    base("warning")(message, duration);
  }
};

export default Message;
